﻿/// <reference path="_references.js" />

(function () {

    // var uploadServerResponseUrl = "/EntryPoint/UploadServerResponse";

    function GetLastRequestData() {
        var element = document.createElement("MyExtensionDataElement");
        document.documentElement.appendChild(element);
        var evt = document.createEvent("Events");
        evt.initEvent("saGetLastRequestDataEvent", true, false);
        element.dispatchEvent(evt);

        var result = {
            hasData: element.getAttribute("hasData"),
            url: element.getAttribute("url"),
            statusCode: element.getAttribute("statusCode"),
            data: element.getAttribute("data"),
        }

        document.documentElement.removeChild(element);
        return result;
    }

    function RetriveData() {
        var requestData = GetLastRequestData();
        if (requestData.hasData && requestData.data) {
            $.post(uploadServerResponseUrl, {
                length: requestData.data.length,
                data: btoa(requestData.data)
            });
            return true;
        }

        return false;
    }

    setInterval(function () {
        while (RetriveData()) { }
    }, 1000);

})();

